﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.util;
using Imaginecup.util.collision;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.sprite;
using imaginecup.util;
using Imaginecup.util.input;
using Microsoft.Xna.Framework.Input.Touch;
using Imaginecup.gameState.temp;

namespace Imaginecup.gameState.btnStateUtil
{
    public class BtnUtil
    {
        private ISelectBtnState m_BtnState;

        public void SetSelectBtnState(ISelectBtnState state)
        {
            m_BtnState = state;
        }


        public void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            DrawBtnInfo[] drawBtnArray = m_BtnState.GetDrawBtnArray();
            for (int i = 0; i < drawBtnArray.Length; i++)
            {
                drawBtnArray[i].Update();
            }

//#if WINDOWS_PHONE
//            IPhoneInputHandler inputHandler = PhoneInputHandler.GetInputHandler();

//            IPhoneInputHandler input = PhoneInputHandler.GetInputHandler();

//            TouchCollection collection = input.GetCurrentTouch();

           
//            if (collection.Count > 0)
//            {
            
//                BtnInfo[] entityInfoArray = m_BtnState.GetBtnInfoArray();

//                foreach (TouchLocation touch in collection)
//                {
//                    if (touch.State == TouchLocationState.Released)
//                    {

//                        for (int i = 0; i < m_BtnState.GetMaxMenu(); i++)
//                        {
//                            if (entityInfoArray[i].GetEnable() && Collision.CollisionByPoint(entityInfoArray[i].GetEntityInfo(), touch.Position))
//                            {
//                                m_BtnState.StateChange(i);         
//                                return;
//                            }
//                        }
//                    }
//                }
//            }
//#endif

//#if WINDOWS|| XBOX
            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

            if(mouseInput.IsOneClickUp())
            {
                BtnInfo[] entityInfoArray = m_BtnState.GetBtnInfoArray();

                for (int i = 0; i < m_BtnState.GetMaxMenu(); i++)
                {
                    if (entityInfoArray[i].GetEnable() && Collision.CollisionByPoint(entityInfoArray[i].GetEntityInfo(), mouseInput.Position) )
                    {
                        m_BtnState.StateChange(i);                        
                        return;
                    }
                }
            }
            
//#endif
        }

      
        public  void Draw(SpriteBatch sb)
        {
            int Max = m_BtnState.GetMaxMenu();
            ISprite[] spriteArray = m_BtnState.GetSpriteArray();
            BtnInfo[] entityInfoArray = m_BtnState.GetBtnInfoArray();

            for (int i = 0; i < Max; i++)
            {
                ISprite sprite = spriteArray[i];
                EntityInfo info = entityInfoArray[i].GetEntityInfo();

                sprite.Draw(sb, info);
            }

        }
    }
}
